#! python3
# randomQuizGenerator.py = creates quizzes with questions and answers in
# random order, along with the answer key

import random

# The quizdata. Keys are states and values are their capitals
capitals = {
    'Bei Jing': 'Bei Jing',
    'Shang Hai': 'Shang Hai',
    'Tian Jin': 'Tian Jin',
    'Si Chuan': 'Cheng Du',
    'Gan Su': 'Lan Zhou',
    'Ji Lin': 'Chang Chun',
    'Hei Long Jiang': 'Ha er Bin',
    'liao Ning': 'Shen Yang',
    'Hu Nan': 'Chang Sha',
    'Hu Bei': 'Wu Han',
    'Xin Jiang': 'Wu Lu Mu Qi',
    'Qing Hai': 'Xi Ning',
    'Ning Xia': 'Yin Chuan',
    'He Bei': 'Shi Jiang Zhuang',
    'Jiang Xi': 'Nan Chang',
    'Jiang Su': 'Nan Jing',
    'Fu Jian': 'Fu Zhou',
    'Guang Xi': 'Nan Ning',
    'Han nan': 'Hai Kou',
    'Gui Zhou': 'Gui Yang'
    }

for quizNum in range(35):
    # Create the quiz and answer key files
    quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')
    answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
    
    # Write out the header for the quiz
    quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
    quizFile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
    quizFile.write('\n\n')
    
    # Shuffle the order of the states
    states = list(capitals.keys())
    random.shuffle(states)
    
    # Loop through all 50 states, making a question for each
    for questionNum in range(20):
        # Get right and wrong answers
        correctAnswer = capitals[states[questionNum]]
        wrongAnswers = list(capitals.values())
        del wrongAnswers[wrongAnswers.index(correctAnswer)]
        wrongAnswers = random.sample(wrongAnswers, 3)
        answerOptions = wrongAnswers + [correctAnswer]
        random.shuffle(answerOptions)
        
        # Write the question and answer options to the quiz file
        quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))
        for i in range(4):
            quizFile.write('  %s. %s\n' % ('ABCD'[i], answerOptions[i]))
        quizFile.write('\n')
        
        # Write the answer key to a file 
        answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
    
    quizFile.close()
    answerKeyFile.close()